package com.jccfc.iMessage.impl;

import com.jccfc.common.HttpClientUtil;
import com.jccfc.common.MessageUtil;
import com.jccfc.iMessage.IMessage;
import com.jccfc.iMessageFactory.ParentMessageFactory;
import com.jccfc.iMessageTo.IMessageTo;
import com.jccfc.iMessageTo.impl.ServerControlMessageTo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Auther: qym
 * @Date: 2021/1/18 15:10
 * @Description: 发送消息到监控系统的功能实例
 */
public class ServerControlMessage implements IMessage {

    //日志打印
    private Logger logger = LoggerFactory.getLogger(ServerControlMessage.class);

    //消息监控服务的ip
    private String httpIp;
    //消息监控服务的端口
    private String httpPort;

    public ServerControlMessage() {
        //初始化参数
        init();
    }

    @Override
    public boolean sendMessage(IMessageTo iMessageTo) {
        //对数据进行校验
        boolean pass = MessageUtil.isPass(iMessageTo, ServerControlMessageTo.class);
        if (!pass) {
            logger.error("消息发送失败,传入参数类型不正确或者为null");
            return false;
        }
        //转化为具体的数据对象
        ServerControlMessageTo serverControlMessageTo = MessageUtil.downcast(iMessageTo, ServerControlMessageTo.class);
        //获取服务监控系统的url路径
        StringBuilder url = new StringBuilder("http://").append(httpIp).append(":").append(httpPort).append("/").append(serverControlMessageTo.getHttpUrl());
        boolean b = HttpClientUtil.doPostToSystem(url.toString(), serverControlMessageTo);
        return b;
    }

    @Override
    public boolean sendMessage(List<IMessageTo> iMessageTos) {
        return false;
    }

    @Override
    public boolean sendScheduleMessage(IMessageTo iMessageTo) {
        return false;
    }

    @Override
    public boolean sendScheduleMessage(List<IMessageTo> iMessageTos) {
        return false;
    }

    @Override
    public void init() {
        httpIp = ParentMessageFactory.getPropertie("system.control.httpIp");
        httpPort = ParentMessageFactory.getPropertie("system.control.httpPort");
    }

}
